#include<vector>
using namespace std;

class Solution {
public:
//超时
    int lastRemaining(int n, int m) {
        vector<int> res(n, 0);
        int pos = 0;

        for (int i = 0; i < n; i++)
        {
            res[i] = i;
        }

        while (res.size()>1)
        {
            int tmp = (pos+m-1)%res.size();
            res.erase(res.begin()+tmp);
            pos=tmp;
        }     
        return res[0];

    }
};